package edu.wfu.preparedDishes.order.controller;

import java.util.List;
import java.util.Map;

import edu.wfu.preparedDishes.order.entity.dto.CreateAddressDto;
import edu.wfu.preparedDishes.order.entity.dto.CreateOderDto;
import edu.wfu.preparedDishes.order.entity.dto.toDelOrderDto;
import edu.wfu.preparedDishes.order.entity.vo.AddressVo;
import edu.wfu.preparedDishes.order.entity.vo.DetailsVo;
import edu.wfu.preparedDishes.order.entity.vo.DishesShowVo;
import edu.wfu.preparedDishes.order.entity.vo.IndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import edu.wfu.preparedDishes.order.entity.po.OrderEntity;
import edu.wfu.preparedDishes.order.service.OrderService;
import edu.wfu.preparedDishes.common.utils.PageUtils;
import edu.wfu.preparedDishes.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 订单表;
 *
 * @author tyy
 * @email tyy2019843259@gmail.com
 * @date 2023-11-21 16:29:10
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 获得该用户所有订单
     * @param params 存放分页信息:page、limit、totalPage
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        System.err.println(userId);
        PageUtils page = orderService.queryPage(params, userId);

        return R.ok().put("orders", page);
    }

    /**
     * 根据orderCode查询该用户订单
     * @param params 存放分页信息:page、limit、totalPage、orderCode
     * @return
     */
    @GetMapping("/list/orderCode")
    public R getOrdersByOrderCode(@RequestParam Map<String, Object> params, HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        PageUtils page = orderService.queryPage(params, userId);

        return R.ok().put("orders", page);
    }

    /**
     * 根据orderStatus查询该用户订单
     * @param params 存放分页信息:page、limit、totalPage、orderCode
     * @return
     */
    @GetMapping("/list/orderStatus")
    public R getOrdersByOrderStatus(@RequestParam Map<String, Object> params, HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        PageUtils page = orderService.queryPage(params, userId);
        return R.ok().put("orders", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{orderCode}")
    public R info(@PathVariable("orderCode") String orderCode){
        OrderEntity order = orderService.getByOrderCode(orderCode);
        return R.ok().put("order", order);
    }

    /**
     * 初始化订单\进入订单页面创建初始订单
     * @return
     */
    @RequestMapping("/create")
    public R create(HttpServletRequest request){
        System.err.println("create");
        Boolean Boolean = orderService.creatOrders(request);
        if (Boolean){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 进入订单页面创建初始订单
     * @param order
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody CreateOderDto order, HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        // 会返回orderCode
        boolean f = orderService.save(order, userId);
        return R.ok();
    }


    /**
     * 创建订单之前的订单详情页面
     * @param userId
     * @return
     */
    @GetMapping("/getBeforeCreatedDetails")
    public R getBeforeCreatedDetails(@RequestParam("userId") String userId,@RequestParam("type") String type,@RequestParam("orderCode") String orderCode, HttpServletRequest request){
        userId = (String) request.getAttribute("userId");
        System.err.println(userId);
        DetailsVo details = orderService.getDetails(userId, orderCode, type);
        if(details != null) {
            return R.ok().put("details", details);
        }
        return R.error();
    }

    /**
     * 取消订单
     * @param delOrderCodes
     * @return
     */
    @DeleteMapping("/cancelOrder")
    public R cancelOrder(@RequestBody List<String> delOrderCodes){
        orderService.cancelOrder(delOrderCodes);
        return R.ok();
    }

    /**
     * 新建地址
     * @param addresses
     * @return
     */
    @PostMapping("/keepAddress")
    public R keepAddress(@RequestBody CreateAddressDto addresses, HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        orderService.keepAddress(addresses, userId);
        return R.ok();
    }

    /**
     * 获得所有收获地址
     * @param userId
     * @return
     */
    @GetMapping("/getAllAddresses")
    public R getAllAddresses(@RequestParam("userId") String userId, HttpServletRequest request){
        userId = (String) request.getAttribute("userId");
        List<AddressVo> allAddresses = orderService.getAllAddresses(userId);
        if(allAddresses != null) {
            return R.ok().put("allAddresses", allAddresses);
        } else {
            return R.error();
        }
    }

    /**
     * 订单完成后获得详细信息
     * @param skuIds
     * @return
     */
    @GetMapping("/getDishesDetails")
    public R getDishesDetails(@RequestParam("skuIds") String skuIds, HttpServletRequest request){
        System.err.println(skuIds);

        String userId = (String) request.getAttribute("userId");
        if(skuIds == "" || skuIds == null) {
            return R.error();
        }
        List<DishesShowVo> dishes = orderService.getDishesDetails(skuIds,userId);
        if(dishes != null) {
            return R.ok().put("dishes", dishes);
        } else {
            return R.error();
        }
    }

    /**
     * 根据skuIds获得所有图片
     * @param skuIds
     * @return
     */
    @GetMapping("/getSrcImages")
    public R getSrcImages(@RequestParam("skuIds") String skuIds){
        if(skuIds == "" || skuIds == null) {
            return R.error();
        }
        List<String> srcs = orderService.getSrcImages(skuIds);
        if(srcs != null) {
            return R.ok().put("srcs", srcs);
        } else {
            return R.error();
        }
    }

    @GetMapping("/getAddressDetail")
    public R getAddressDetail(@RequestParam("addressId") String addressId){
        if(addressId == "" || addressId == null) {
            return R.error();
        }
        AddressVo address = orderService.getAddressDetail(addressId);
        if(address != null) {
            return R.ok().put("address", address);
        } else {
            return R.error();
        }
    }

    /**
     * 首页显示待收货的信息（默认只有一个待收货，且不考虑没有待收货情况）
     * @param userId
     * @return
     */
    @GetMapping("/indexShow")
    public R indexShow(@RequestParam("userId") String userId, HttpServletRequest request){
        userId = (String) request.getAttribute("userId");
        IndexVo DishesShowVo = orderService.getIndexShow(userId);
        if(DishesShowVo != null) {
            return R.ok().put("indexDetails", DishesShowVo);
        } else {
            return R.error();
        }
    }

    @GetMapping("/receivin")
    public R receivin(@RequestParam("orderCode") String orderCode){
        if(orderService.receivin(orderCode)){
            return R.ok();
        }else {
            return R.error();
        }

    }



}
